package com.yanqu.road.server.channelserver.cmd.activity.pasture;

import com.yanqu.road.entity.activity.entername.ActivityGroupMatch;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.PastureProto;
import com.yanqu.road.pb.cross.activity.CrossPastureProto;
import com.yanqu.road.server.channelserver.cmd.ChannelCommand;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameRankMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameRankModel;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = ChannelProtocol.N_PASTURE_ACTIVITY_GAME_OVER, desc = "游戏结束获取排名")
public class ChannelPastureGameOverCmd extends ChannelCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossPastureProto.ChannelPastureGetRankReqMsg reqMsg = CrossPastureProto.ChannelPastureGetRankReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        PastureProto.CrossPastureGetRankRespMsg.Builder gameMsg = PastureProto.CrossPastureGetRankRespMsg.newBuilder();
        gameMsg.setActivityId(activityId);
        gameMsg.setIsPractice(reqMsg.getIsPractice());
        gameMsg.setRank(-1);
        long userId = reqMsg.getUserKey().getUserId();
        ActivityGroupMatch groupMatch = ChannelEnterNameMgr.getActivityGroupMatch(activityId, userId);
        if(groupMatch != null){
            EnterNameRankModel model = ChannelEnterNameRankMgr.getEnterNameRankModel(activityId, eGamePlayerEventType.ChannelPastureMaxScoreRank.getValue(), groupMatch.getRankMatchGroupId());
            if(model != null){
                gameMsg.setRank(model.getMyTempRank(userId, reqMsg.getScore()));
            }
        }
        ChannelMessageHelper.sendGamePacket(Protocol.S_CHANNEL_CROSS_PASTURE_GAME_OVER, gameMsg, reqMsg.getUserKey().getChannelId(),
                reqMsg.getUserKey().getServerId(), userId);
    }

    @Override
    public void sendData() {

    }
}
